package com.system.controller;
import com.system.po.Pinke;
import com.system.service.PinkeService;
import com.system.utils.BaseController;
import com.system.utils.excelutil.ExcelUtil;
import com.system.utils.excelutil.WDWUtil;
import com.system.utils.poutils.PinkeUtil;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
/**
 * @author zchome
 */
@Controller
public class OcrController extends BaseController {
	private ResourceLoader resourceLoader;
	private String path = "";
	String localPath = "E:/a/";
	/*------------*/
	/**
	 * 123
	 */
	@Resource(name = "pinkeServiceImpl")
	private PinkeService pinkeService;
	/*------------*/
	@RequestMapping(value = "/card", method = {RequestMethod.GET})
	public String card(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
		return "../../index";
	}
	@ResponseBody
	@RequestMapping("/uploadImg.xhtml")
	public void uploadPicture(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response, Model model) {
		response.setContentType("text/json;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		try {
			try {
				InputStream is;
				String fileName1 = file.getOriginalFilename();
				boolean isExcel2003 = true;
				if (WDWUtil.isExcel2007(fileName1)) {
					isExcel2003 = false;
				}
				is = file.getInputStream();
				ExcelUtil excelUtil = new ExcelUtil();
				List<List<Object>> dataList = excelUtil.readlog(is, isExcel2003);
				System.out.println("size:" + dataList.size());
				List<Pinke> pinkerList = new ArrayList<Pinke>();
				for (List<Object> aDataList : dataList) {
					Pinke pinke = new Pinke();
					pinke.setBusno(new BigDecimal(aDataList.get(2).toString().replace("`", "")).intValue());
					pinke.setOpfare(new BigDecimal(aDataList.get(5).toString().replace("`", "")).multiply(new BigDecimal(100)).intValue());
					pinke.setCurrstatues(0);
					pinke.setTransid((aDataList.get(1).toString().replace("`", "")));
					pinkerList.add(pinke);
					System.out.println(pinke.toString());
				}
				int insRet = pinkeService.batchInsert(pinkerList);
				System.out.println("insRet:" + insRet);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/record")
	public void record(Integer busNo, Integer cardNo, BigDecimal opFare, Integer type, Integer currstatues, HttpServletRequest request, HttpServletResponse response, Model model) {
		//		System.out.println("busNo:" + busNo);
		//		System.out.println("cardNo:" + cardNo);
		//		System.out.println("opFare:" + opFare);
		//		System.out.println("currstatues:" + currstatues);
		//		System.out.println("type:" + type);
		if (type == 0) {
			//			Pinke pinke = new Pinke();
			//			pinke.setBusno(busNo);
			//			pinke.setCardno(cardNo);
			//			pinke.setOpfare(opFare.intValue());
			//			pinke.setCurrstatues(currstatues);
			int s = pinkeService.updateByBusNoOpFare(busNo, opFare.multiply(new BigDecimal(100)).intValue(), cardNo);
			System.out.println("insert result:" + s);
			writeJson(response, s);
		}
		else if (type == 1) {
			List<Pinke> pinkesList = pinkeService.selectAll();
			for (Pinke pinke : pinkesList) {
				pinke.setOpfare(new BigDecimal(pinke.getOpfare().toString()).divide(new BigDecimal(100), 2, 4).intValue());
			}
			writeJson(response, pinkesList);
		}
		else if (type == 2) {
			Pinke pinke = pinkeService.selectByBusNo(busNo);
			System.out.println(pinke);
			if (pinke != null) {
				pinke.setOpfare(new BigDecimal(pinke.getOpfare().toString()).divide(new BigDecimal(100), 2, 4).intValue());
				System.out.println(pinke.toString());
			}
			writeJson(response, pinke);
		}
	}
	@RequestMapping("/showResult")
	public String showResult(HttpServletRequest request, HttpServletResponse response, Model model) {
		List<Map<Object, Object>> allList = pinkeService.selectAllHandlInfo(130);
		System.out.println("allList:" + allList);
		for (Map<Object, Object> m : allList) {
			m.put("opfare", new BigDecimal(m.get("opfare").toString()).divide(new BigDecimal(100), 2, 4));
			m.put("currstatuesDes", PinkeUtil.setCurrstatuesDesc(new BigDecimal(m.get("currstatues").toString()).intValue()));
		}
		System.out.println("allList:" + allList);
		model.addAttribute("allList", allList);
		return "pinke/result";
	}
}
